package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.IpUtil;
import com.atguigu.gmall.model.user.vo.LoginRespVo;
import com.atguigu.gmall.model.user.vo.LoginVo;
import com.atguigu.gmall.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:lyd
 **/
@RestController
@RequestMapping("/api/user")
public class RestLoginController {

    @Autowired
    UserInfoService userInfoService;

    @PostMapping("/passport/login")
    public Result login(@RequestBody LoginVo loginVo,
                        HttpServletRequest request) {
        String deviceId = IpUtil.getIpAddress(request);
        LoginRespVo loginRespVo = userInfoService.login(loginVo,deviceId);
        return Result.ok(loginRespVo);
    }

    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("Token") String token) {
        userInfoService.logout(token);
        return Result.ok();
    }
}
